package com.example.composableexample.drawer

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Divider
import androidx.compose.material3.DrawerDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorFilter.Companion.tint
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composableexample.R
import kotlinx.coroutines.launch

/**
 * author: LiLiang
 * createTime: 2022/9/30-19:43
 * remark:
 **/

val screens = listOf(
    AppScreens.HomeScreen,
    AppScreens.AccountScreen,
    AppScreens.ContactScreen,
    AppScreens.HelpScreen
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DrawerScreen(
    modifier: Modifier = Modifier,
    onDestinationClicked: (route: String) -> Unit
) {
    val selectedItem = remember { mutableStateOf(screens[0]) }
    val scope = rememberCoroutineScope()
    Column(
        modifier = modifier
            .width(DrawerDefaults.MaximumDrawerWidth)
            .background(DrawerDefaults.containerColor)
    ) {
        Column(
            modifier = modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(id = R.drawable.head),
                contentDescription = "App icon",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .padding(top = 16.dp)
                    .size(140.dp)
                    .clip(RoundedCornerShape(50)),
            )
        }
        ModalDrawerSheet(modifier = Modifier.padding(top = 68.dp)) {
            screens.forEach { screen ->
                NavigationDrawerItem(
                    icon = {
                        Icon(
                            imageVector = screen.icon,
                            contentDescription = screen.title,
                            tint = MaterialTheme.colorScheme.onPrimary
                        )
                    },
                    label = {
                        Text(
                            text = screen.title,
                            style = MaterialTheme.typography.titleMedium
                        )
                    },
                    selected = screen == selectedItem.value,
                    onClick = {
                        scope.launch {
                            onDestinationClicked(screen.route)
                            selectedItem.value = screen
                        }
                    }
                )
            }
            Spacer(modifier = Modifier.weight(1f))
            Text(
                text = "不涸泽而渔，\n不焚林而猎。",
                color = Color.Gray,
                textAlign = TextAlign.Start,
                style = MaterialTheme.typography.bodyLarge,
                modifier = Modifier
                    .padding(12.dp)
                    .align(Alignment.CenterHorizontally)
            )
        }
//        Box(modifier = Modifier.padding(top = 128.dp)) {
//            Column {
//                screens.forEach { screen ->
//                    Row(
//                        verticalAlignment = Alignment.CenterVertically,
//                        modifier = Modifier
//                            .height(48.dp)
//                            .width(DrawerDefaults.MaximumDrawerWidth)
//                            .padding(
//                                horizontal = 16.dp
//                            )
//                            .clickable {
//                                onDestinationClicked(screen.route)
//                            }
//                    ) {
//                        Icon(
//                            imageVector = screen.icon,
//                            contentDescription = screen.title,
//                            tint = MaterialTheme.colorScheme.onPrimary
//                        )
//                        Spacer(modifier = Modifier.width(24.dp))
//                        Text(text = screen.title, style = MaterialTheme.typography.titleMedium)
//                    }
//                    Divider(
//                        color = MaterialTheme.colorScheme.onBackground,
//                        modifier = Modifier.padding(horizontal = 16.dp)
//                    )
//                }
//                Spacer(modifier = Modifier.weight(1f))
//                Text(
//                    text = "不涸泽而渔，\n不焚林而猎。",
//                    color = Color.Gray,
//                    textAlign = TextAlign.Start,
//                    style = MaterialTheme.typography.bodyLarge,
//                    modifier = Modifier
//                        .padding(12.dp)
//                        .align(Alignment.CenterHorizontally)
//                )
//            }
//        }
    }
}

@Preview
@Composable
fun DrawerScreenPreview() {
    DrawerScreen(onDestinationClicked = {})
}